package com.zbaohuang.fragment;


import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.VideoView;

import com.androidadvance.topsnackbar.TSnackbar;
import com.zbaohuang.activity.VideoManagerActivity;
import com.zbaohuang.view.SquareLayout;
import com.zbaohuang.xyh.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class VideoLiveFragment extends Fragment {


    @Bind(R.id.surface_view)
    VideoView surfaceView;
    @Bind(R.id.line1)
    View line1;
    @Bind(R.id.firstLayout)
    SquareLayout firstLayout;
    @Bind(R.id.line2)
    View line2;
    @Bind(R.id.secondLayout)
    SquareLayout secondLayout;
    @Bind(R.id.line3)
    View line3;
    @Bind(R.id.threeLayout)
    SquareLayout threeLayout;
    @Bind(R.id.line4)
    View line4;
    @Bind(R.id.fourLayout)
    SquareLayout fourLayout;

    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
//            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//            builder.setTitle("系统提示");
//            builder.setMessage("老人跌倒请紧急处理");
//            builder.setPositiveButton("确定",
//                    new DialogInterface.OnClickListener() {
//                        public void onClick(DialogInterface dialog, int whichButton) {
//                        }
//                    });
//            builder.show();

            TSnackbar snackbar = TSnackbar
                    .make(getActivity().findViewById(android.R.id.content), "老人跌倒请紧急处理", TSnackbar.LENGTH_LONG)
                    .setAction("紧急呼叫", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            Uri uri = Uri.parse("tel:15989324626");
                            Intent it = new Intent(Intent.ACTION_DIAL, uri);
                            startActivity(it);

                        }
                    });
            snackbar.setActionTextColor(Color.RED);
            snackbar.show();

            showNotification();
        }
    };


    private void showNotification(){
        Notification noti = new Notification.Builder(getActivity())
                .setContentTitle("夕阳红")
                .setContentText("老人跌倒请紧急处理")
                .setSmallIcon(R.mipmap.ic_launcher)
                .build();
        noti.flags = Notification.FLAG_ONGOING_EVENT; // 设置常驻 Flag
        // 设置属性值
        noti.when = System.currentTimeMillis(); // 立即发生此通知
        noti.defaults |= Notification.DEFAULT_SOUND;
        noti.defaults |= Notification.DEFAULT_VIBRATE;
        // 添加状态标志
        // FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉
        // FLAG_NO_CLEAR 该通知能被状态栏的清除按钮给清除掉
        // FLAG_ONGOING_EVENT 通知放置在正在运行
        // FLAG_INSISTENT 通知的音乐效果一直播放
        noti.flags = Notification.FLAG_AUTO_CANCEL;

        // 将该通知显示为默认View

        // 设置setLatestEventInfo方法,如果不设置会App报错异常
        NotificationManager mNotificationManager = (NotificationManager)getActivity().getSystemService(Context.NOTIFICATION_SERVICE);

        // 注册此通知
        // 如果该NOTIFICATION_ID的通知已存在，会显示最新通知的相关信息 ，比如tickerText 等
        mNotificationManager.notify(12312, noti);
    }

    public VideoLiveFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_video_live, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setHasOptionsMenu(true);
        handler.postDelayed(runnable, 10000);
        startPlay(R.raw.keting);
    }

    private void startPlay(int res){

        if (surfaceView.isPlaying()){
            surfaceView.stopPlayback();
        }

        Uri uri = Uri.parse("android.resource://com.zbaohuang.xyh/" +res);
        surfaceView.setVideoURI(uri);
        surfaceView.requestFocus();
        surfaceView.start();
        surfaceView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                surfaceView.start();
                handler.removeCallbacks(runnable);
                handler.postDelayed(runnable, 10000);
            }
        });
    }


    @Override
    public void onPause() {
        super.onPause();

        handler.removeCallbacks(runnable);

    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu_analysis, menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {


        if (item.getItemId() == R.id.menu_manager) {

            Intent intent = new Intent(getActivity(), VideoManagerActivity.class);
            startActivity(intent);


            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.firstLayout, R.id.secondLayout, R.id.threeLayout, R.id.fourLayout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.firstLayout:
            {
                startPlay(R.raw.keting);
                line1.setVisibility(View.VISIBLE);
                line2.setVisibility(View.INVISIBLE);
                line3.setVisibility(View.INVISIBLE);
                line4.setVisibility(View.INVISIBLE);
            }
                break;
            case R.id.secondLayout:
            {
                startPlay(R.raw.woshi);
                line2.setVisibility(View.VISIBLE);
                line1.setVisibility(View.INVISIBLE);
                line3.setVisibility(View.INVISIBLE);
                line4.setVisibility(View.INVISIBLE);
            }
                break;
            case R.id.threeLayout:
            {
                startPlay(R.raw.menkou);
                line3.setVisibility(View.VISIBLE);
                line2.setVisibility(View.INVISIBLE);
                line1.setVisibility(View.INVISIBLE);
                line4.setVisibility(View.INVISIBLE);
            }
                break;
            case R.id.fourLayout:
            {
                startPlay(R.raw.chufang);
                line4.setVisibility(View.VISIBLE);
                line2.setVisibility(View.INVISIBLE);
                line3.setVisibility(View.INVISIBLE);
                line1.setVisibility(View.INVISIBLE);
            }
                break;
        }
    }
}
